#!/bin/sh
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
#getargbool() { true; }

if getargbool 0 initgrub && [ ! -e /grub2skip ] || [ -e /grub2force ]; then
  #type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
  checkro() {
   local tgt="$1"
   local dev mp fs opts dc
   local rofs=true
   while read dev mp fs opts dc; do
     [ "$mp" = "$tgt" ] || continue
     case ",$opts," in
     (*,ro,*) rofs=true;;
     (*)      rofs=false;;
     esac
   done < /proc/mounts
   echo $rofs
  }
  checkcat() {
    [ -r $1 ] && cat $1
  }
  checkd() {
    [ -d $1 ] && echo true || echo false
  }
  checke() {
    [ -e $1 ] && echo true || echo false
  }
  checksubvol() {
    local tgt="$1"
    local tst="$2"
    if [ -n "$tst" -a -e "/sysroot$tgt/$tst" ]; then
      echo true
    elif grep  -qP '^[^#\s]+\s+'"$tgt"'\s+' /sysroot/etc/fstab; then
      echo false
    else
      echo true
    fi
  }
  checksnap() {
    # checksubvol /.snapshots grub-snapshot.cfg
    if [ -e /sysroot/.snapshots/grub-snapshot.cfg ]; then
      echo true
    elif grep  -qP '^[^#\s]+\s+/.snapshots\s+' /sysroot/etc/fstab; then
      echo false
    else
      echo true
    fi
  }
  checkboot() {
    [ -d /boot/grub2 ] && echo false || echo true
  }
  getterm() {
    local term="$(getarg TERM)"
    [ -z "$term" ] && term=dumb
    echo $term
  }
  debug() {
    if [ -n "$1" ]; then
      echo "$1" >> /dev/.grub2.debug
    fi
    shift;
    [ -n "$*" ] || return 0
    echo "+ $*" >> /dev/.grub2.debug
    "$@" >> /dev/.grub2.debug
  }

  exec_prefix=@exec_prefix@
  bindir=@bindir@
  if [ -e /sysroot$bindir/grub2-emu ]; then

    export TERM=$(getterm)
    export grub2rofs=$(checkro /sysroot)
    export grub2roufs=$(checkro /sysroot/usr)
    export grub2sysfs=$(checkd /sysroot/sys/devices/system/memory)
    export grub2procfs=$(checkd /sysroot/proc/self)
    export grub2bootfs=$(checkboot)
    export grub2bootw=$(checksubvol /boot/writable)
    export grub2devfs=$(checkd /sysroot/dev/disk)
    export grub2snap=$(checksnap)
    export grub2secure=$(checkcat /sys/firmware/ipl/secure)
    debug "" export -p

        _ctty="$(RD_DEBUG= getarg rd.ctty=)" && _ctty="/dev/${_ctty##*/}"
        if [ -z "$_ctty" ]; then
	    _ctty=$(showconsole)
	fi
        if [ -z "$_ctty" ]; then
            _ctty=console
            while [ -f /sys/class/tty/$_ctty/active ]; do
                _ctty=$(cat /sys/class/tty/$_ctty/active)
                _ctty=${_ctty##* } # last one in the list
            done
            _ctty=/dev/$_ctty
        fi
        [ -c "$_ctty" ] || _ctty=/dev/console
        case "$(/usr/bin/setsid --help 2>&1)" in *--ctty*) CTTY="--ctty";; esac

    CTTY="$CTTY --wait"
    $grub2rofs || mount -o remount,ro /sysroot
    $grub2roufs || mount -o remount,ro /sysroot/usr
    $grub2sysfs || mount --bind {,/sysroot}/sys
    $grub2procfs || mount --bind {,/sysroot}/proc
    $grub2bootfs || mount --bind {,/sysroot}/boot
    $grub2devfs || mount --bind {,/sysroot}/dev
    $grub2snap || chroot /sysroot mount -rn /.snapshots
    $grub2bootw || chroot /sysroot mount -rn /boot/writable
    debug "" cat /proc/mounts

    debug "Trying grub2-emu (ro=$grub2rofs, TERM=$TERM, ctty=$_ctty)..."
    setsid $CTTY -- chroot /sysroot $bindir/grub2-emu -X -X 0<>$_ctty 1>&0 2>&0

    if [ "$grub2secure" != 1 ]&&[ -x /sysroot@libdir@/grub2/zipl-refresh ]; then
	setsid $CTTY -- /sysroot@libdir@/grub2/zipl-refresh 0<>$_ctty 1>&0 2>&0
	if [ $? != 0 ]; then
	    warn "Not continuing"
	    emergency_shell -n grub2-emu-zipl-refresh
	else
	    echo "+ reboot" >& $_ctty
	    sleep 3
	    reboot
	fi
    elif [ "$grub2secure" != 1 ]; then
	echo "
  Attention: 'grub2' failed to start the target kernel and 'zipl-refresh'
  is not available.  This should never happen.  Please contact support." >& $_ctty
	warn "Not continuing"
	emergency_shell -n grub2-emu-kexec
    else
	echo "
  Attention: 'grub2' failed to start the target kernel and secure boot seems
  active.  Automatic recovery not available.  Please contact support." >& $_ctty
	warn "Not continuing"
	emergency_shell -n grub2-emu-kexec
    fi

    $grub2snap || umount /sysroot/.snapshots
    $grub2devfs || umount /sysroot/dev
    $grub2bootw || umount /sysroot/boot/writable
    $grub2bootfs || umount /sysroot/boot
    $grub2procfs || umount /sysroot/proc
    $grub2sysfs || umount /sysroot/sys
    $grub2roufs || mount -o remount,rw /sysroot/usr
    $grub2rofs || mount -o remount,rw /sysroot
  else
    warn "No $bindir/grub2-emu in /sysroot--dropping to emergency shell..."
    emergency_shell -n no-grub2-emu
  fi
fi

